package cluster

import (
	"context"
	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"krm_backend/config"
	"krm_backend/utils/logs"
)

func Delete(r *gin.Context) {
	clusterId := r.Query("clusterID")
	returnData := config.NewReturnData()
	// 2. 删除
	err := config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Delete(context.TODO(), clusterId, metav1.DeleteOptions{})
	if err != nil {
		// 集群删除失败
		logs.Error(map[string]interface{}{"Id": clusterId}, "删除失败")
		msg := "集群删除失败: " + err.Error()
		returnData.Status = 400
		returnData.Message = msg
	} else {
		// 删除成功
		logs.Warning(nil, "删除成功")
		returnData.Status = 200
		returnData.Message = "删除成功"
	}
	// 删除配置信息
	delete(config.ClusterKubeconfig, clusterId)
	r.JSON(200, returnData)
}
